<!--
 * @Description: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2022-01-10 09:36:46
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2022-01-10 09:57:37
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>删除链表节点</title>
</head>

<body>
    <script>
        // 双指针的经典应用，如果要删除倒数第n个节点，让fast移动n步，然后让fast和slow同时移动，直到fast指向链表末尾。删掉slow所指向的节点就可以了。

        var removeNthFromEnd = function (head, n) {
            let ret = new ListNode(0, head);
            let slow = fast = ret;
            while (n--) {
                fast = fast.next;
            }
            while (fast.next != null) {
                fast = fast.next;
                slow = slow.next;
            }
            slow.next = slow.next.next;
            return ret.next;
        }
    </script>

</body>

</html>